﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Mail;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

public partial class Admin_Mail_SendEmail : System.Web.UI.Page
{
    SqlConnection con;
    string MailID;
    //Connect to database
    void connect()
    {
        con = new SqlConnection(ConfigurationManager.ConnectionStrings["OPOConnectionString"].ToString());
        con.Open();
        //if (con != null)
        //{
        //    Response.Write("Connect Successful.");
        //}

    }
    //Update Column isSendEmail of Mail to "Yes"
   // 
    void updateSendEmail() 
    {
        try
        {
            string sqlUpdateSendEmail = "UPDATE [Mail] SET [isSendEmail] = @isSendEmail WHERE [MailID] = @MailID";
            SqlCommand cmdUpdate = new SqlCommand(sqlUpdateSendEmail, con);
            cmdUpdate.Parameters.AddWithValue("MailID", Convert.ToString(Session["IDEmail"]));
            cmdUpdate.Parameters.AddWithValue("@isSendEmail","Yes");
            int Count = cmdUpdate.ExecuteNonQuery();
            if (Count > 0)
            {
                MessageDialog.MessDialog("Change Send Mail to \"Yes\" Completed", this);
            }
            else
            {
                MessageDialog.MessDialog("Change Send Mail to \"Yes\" Failed", this);
            }
        }
        catch (Exception e)
        {
            MessageDialog.MessDialog(e.Message, this);
        }
    }
    //Send email to customer
    void sendEmail() 
    {  
   
                    try
                    {
                        SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
                        client.EnableSsl = true; //Allow Server mail use SSL
                        client.Credentials = new NetworkCredential(txtEmailG.Text, txtPassword.Text);
                        using (MailMessage msg = new MailMessage())
                        {
                            msg.From = new MailAddress(txtEmailG.Text);
                            msg.Subject = txtSubject.Text;
                            msg.Body = txtContent.Text;
                            msg.To.Add(new MailAddress(lbEmailTo.Text));
                            client.Send(msg);

                            MessageDialog.MessDialog("Send email to "+lbEmailTo.Text+" successful.", this);
                            
                        }
                    }
                    catch (Exception c)
                    {
                        MessageDialog.MessDialog("Invalid Email or Password", this);
                        return;
                    }
                
        
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        connect();
        MailID = Convert.ToString(Session["IDEmail"]);
        lbEmailTo.Text = Convert.ToString(Session["Email"]);
        txtContent.Text = "Hi "+Session["SenderName"]+"\nYour mail received";

    }
    protected void txtPassword0_TextChanged(object sender, EventArgs e)
    {

    }

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {

    }
    protected void imgbSend_Click(object sender, ImageClickEventArgs e)
    {
        sendEmail();
        updateSendEmail();
    }
    protected void imgBack_Click(object sender, ImageClickEventArgs e)
    {
         Response.Redirect("Mail_Details.aspx?MailID=" + MailID);
    }
}